//JavaScript
var registerSession;
var respVideos;

function setRegisterSession(registerSessionToSet){
  this.registerSession = registerSessionToSet;
}

function getVideos(){
  respVideos = false;
  var url = 'include/bd/videos_bd.php';
  $.getJSON(url
        ,gestionReponseVideos);
        
  setTimeout(function(){
  if (!respVideos){
      $('#videos').html("<DIV ALIGN='center'><IMG SRC='images/loading.gif' ALIGN='center'></DIV>");
  }
  },100);
  
}

function gestionReponseVideos(videos)
{
  respVideos = true;
   if (videos){
      if (videos.length > 0)
      {
          var html = "";
          var catOld = -1;
          var array = new Array();
          var catArray = null;
          for (var i =0 ; i < videos.length; i++){
            var video = videos[i];
            if (video.idType != catOld){
                if (catOld != -1){
                  array.push(catArray);
                }
                catOld = video.idType;
                catArray = new Array();
            }
            catArray.push(video);
          }
          array.push(catArray);
          
          // affichage des donées
          for (var i=0; i< array.length ; i++){
                html += afficheCategorieVideo(array[i]);
          }
          $('#videos').html(html);
      }
    }
}

function afficheCategorieVideo(videos){
 //CREATION DU SQUELETE HTML
var html = ""+
"<DIV id='video_top'>"+
  	html_entity_decode(videos[0].nomCat)+				
	"</TABLE>"+
"</DIV>"+
"<DIV id='video_content_back'>"+
	"<DIV id='video_content'>";
		// INJECTATION DES DONNEES
		for (var i = 0; i < videos.length; i ++){
  		  var video = videos[i];
        if (i > 0){
          html += "<HR width='600px'/>";
        }
         if (video.idImg != null){
      			html += ""+
            "<TABLE ALIGN='top'>"+
      			"<TR><TD WITDTH ='200px'>"+
      			"<A HREF='videos/"+video.urlVideo+"' target='_blank' class='blanc'><IMG WIDTH='200px' SRC='illustrations/"+video.illustrations[0].url+"'></A></BR>"+
      			"</TD><TD WIDTH='550px'>";
    		 }
		 html += ""+
		 "<A HREF='videos/"+video.urlVideo+"' target='_blank' class='blanc'>"+html_entity_decode(video.titre)+"</A> : </BR></BR>"+
		 "Description : "+html_entity_decode(video.desc)+" </BR></BR>";
		 if (video.taille != null){
		 	  html += "<B>Taille = </B>"+video.taille+" Mo </BR>";
		 }
		 if (video.duree != null){
		 	  html += "<B>Temps : </B>"+video.duree+" </BR>";
		 }
		 if (video.idImg != null){
  		  html += "</TD></TR>"+
  			"</TABLE>";
		  }
		  if (registerSession == 1) {
        html +=""+ 
				"<A class='blanc' HREF='index.php?page=update_video&id_video="+video.idVideo+"'>Editer cette vidéo</A> -"; 
		  }
    }
	// Poursuite du squelette HTML
	html += ""+
	"</DIV>"+
"</DIV>"+
"<DIV id='video_content_end' />"+
"<BR />";	
return html;
}
	

	
